<template>
  <div class="layout">
    <!-- 左侧菜单 -->
    <div class="sidebar">
      <div class="logo">
        <h3>管理系统</h3>
      </div>
      
      <el-menu
        :default-active="activeMenu"
        class="sidebar-menu"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
        :unique-opened="true"
        router
      >
        <!-- 用户管理菜单 -->
        <el-sub-menu index="user-manage" v-if="hasAnyPermission(['user:read', 'role:read'])">
          <template #title>
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </template>
          
          <PermissionGuard permission="role:read">
            <el-menu-item index="/dashboard/roles">
              <el-icon><UserFilled /></el-icon>
              <span>角色控制</span>
            </el-menu-item>
          </PermissionGuard>
          
          <PermissionGuard permission="user:read">
            <el-menu-item index="/dashboard/users">
              <el-icon><Avatar /></el-icon>
              <span>权限控制</span>
            </el-menu-item>
          </PermissionGuard>
        </el-sub-menu>

        <!-- 爬虫菜单 -->
        <el-sub-menu index="spider-manage">
          <template #title>
            <el-icon><Promotion /></el-icon>
            <span>爬虫菜单</span>
          </template>
          
          <el-menu-item index="/dashboard/spider/tasks">
            <el-icon><List /></el-icon>
            <span>爬虫任务</span>
          </el-menu-item>
          
          <el-menu-item index="/dashboard/spider/data">
            <el-icon><DataBoard /></el-icon>
            <span>数据管理</span>
          </el-menu-item>
        </el-sub-menu>

        <!-- 服务器性能监控 -->
        <PermissionGuard permission="system:monitor">
          <el-menu-item index="/dashboard/server-monitor">
            <el-icon><Monitor /></el-icon>
            <span>服务器性能监控</span>
          </el-menu-item>
        </PermissionGuard>
      </el-menu>
    </div>

    <!-- 右侧内容区域 -->
    <div class="main-container">
      <!-- 顶部导航 -->
      <div class="header">
        <div class="breadcrumb">
          <div class="breadcrumb-left">
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
              <template v-if="breadcrumbItems.length > 0">
                <el-breadcrumb-item 
                  v-for="item in breadcrumbItems" 
                  :key="item.path" 
                  :to="item.path ? { path: item.path } : null">
                  {{ item.name }}
                </el-breadcrumb-item>
              </template>
            </el-breadcrumb>
          </div>
          <div class="current-time">
            {{ currentTime }}
          </div>
        </div>
        
        <div class="header-right">
          <el-dropdown>
            <span class="user-dropdown">
              <el-avatar :size="30" :src="userAvatar" />
              <span class="username">{{ currentUser?.username }}</span>
              <el-icon><CaretBottom /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>个人设置</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>

      <!-- 主内容区域 -->
      <div class="main-content">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { 
  User, UserFilled, Avatar, Promotion, List, DataBoard, CaretBottom, Monitor 
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useAuth } from '@/composables/useAuth'
import PermissionGuard from '@/components/PermissionGuard.vue'

const route = useRoute()
const router = useRouter()
const { currentUser, logout, hasAnyPermission } = useAuth()

const userAvatar = ref('https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png')
const currentTime = ref('')

let timer = null

const updateTime = () => {
  const now = new Date()
  const year = now.getFullYear()
  const month = String(now.getMonth() + 1).padStart(2, '0')
  const day = String(now.getDate()).padStart(2, '0')
  const hours = String(now.getHours()).padStart(2, '0')
  const minutes = String(now.getMinutes()).padStart(2, '0')
  const seconds = String(now.getSeconds()).padStart(2, '0')
  
  currentTime.value = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

onMounted(() => {
  updateTime()
  timer = setInterval(updateTime, 1000)
})

onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
  }
})

// 当前激活的菜单项
const activeMenu = computed(() => {
  return route.path
})

// 面包屑导航
const breadcrumbItems = computed(() => {
  const items = []
  const path = route.path
  
  if (path.includes('/dashboard/users')) {
    items.push({ name: '权限控制', path: null })
  } else if (path.includes('/dashboard/roles')) {
    items.push({ name: '角色控制', path: null })
  } else if (path.includes('/dashboard/spider/tasks')) {
    items.push({ name: '爬虫任务', path: null })
  } else if (path.includes('/dashboard/spider/data')) {
    items.push({ name: '数据管理', path: null })
  } else if (path.includes('/dashboard/server-monitor')) {
    items.push({ name: '服务器性能监控', path: null })
  }
  
  return items
})

const handleLogout = () => {
  logout()
  ElMessage.success('已退出登录')
  router.push('/login')
}
</script>

<style scoped>
.layout {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 250px;
  height: 100vh;
  background-color: #304156;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
}

.main-container {
  flex: 1;
  margin-left: 250px;
  display: flex;
  flex-direction: column;
}

.logo {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid #434a50;
}

.logo h3 {
  color: #fff;
  margin: 0;
  font-size: 18px;
}

.sidebar-menu {
  border: none;
  height: calc(100vh - 80px);
  overflow-y: auto;
}

.sidebar-menu:not(.el-menu--collapse) {
  width: 250px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  height: 60px;
  flex-shrink: 0;
}

.breadcrumb {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.breadcrumb-left {
  flex: 1;
}

.current-time {
  color: #409EFF;
  font-size: 14px;
  font-weight: 500;
  margin-left: 20px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-dropdown:hover {
  background-color: #f5f5f5;
}

.username {
  margin: 0 8px;
  font-size: 14px;
}

.main-content {
  background: #f0f2f5;
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}

/* 滚动条样式 */
.sidebar-menu::-webkit-scrollbar {
  width: 6px;
}

.sidebar-menu::-webkit-scrollbar-thumb {
  background: #434a50;
  border-radius: 3px;
}

.sidebar-menu::-webkit-scrollbar-thumb:hover {
  background: #4a5568;
}
</style>
